package com.xiaoyuanxing.demo.controller;

import com.xiaoyuanxing.demo.entity.Account;
import com.xiaoyuanxing.demo.entity.AccountInfo;
import com.xiaoyuanxing.demo.service.AccountService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/*
 **@author LuoShengYing
 **@date 2021/5/23
 */

@RestController
@RequestMapping("Account")
public class AccountController {
    @Resource(name = "accountService")
    private AccountService accountService;

    /*
    * 登录
    * */
    @RequestMapping("login")
    public String login(String openid){
        // 获取openID对应的Account数据
        Account account = accountService.selectByOpenID(openid);
        // 如果数据为空，则说明数据库中不存在对应openID的Account数据
        if (ObjectUtils.isEmpty(account)){
            return "不存在此用户";
        }
        return "登录成功";
    }


    /*
    * 注册
    * */
    @RequestMapping("register")
    public String register(String openid,String avatarUrl, String nickname, String sex, String studentID, String trueName, int schoolID, String collegeName){
        int status = accountService.register(openid, avatarUrl, nickname, sex, studentID, trueName, schoolID, collegeName);
        // 如果状态为-1，则说明数据库中存在相同的openID数据
        if (status == -1){
            return "存在相同openID的数据";
        }
        if (status == 1){
            return "注册成功";
        }
        return "注册失败";
    }


    /*
    * 获取账户详情信息
    * */
    @RequestMapping("getAccountInfo")
    public AccountInfo getAccountInfo(String openid){
        // 通过openId查询对应的Account数据
        Account account =  accountService.selectByOpenID(openid);
        // 通过accountID查询对应的AccountInfo数据，并返回
        return accountService.selectAccountInfoByAccountID(account.getAccountID());
    }
}
